package com.kaishengit.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Contact;
import com.kaishengit.entity.Record;
import com.kaishengit.entity.User;
import com.kaishengit.service.ContactService;
import com.kaishengit.util.Page;
import com.kaishengit.util.ZxingEncoderHandler;
public class ContactServlet extends HttpServlet {
	private ContactService service = new ContactService();
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method == null || "".equals(method)){
			toContact(request,response);
		}else if("singleContact".equals(method)){
			toSingleContact(request,response);
		}else if("toAllRecord".equals(method)){
			toAllRecord(request,response);
		}else if("publishRecord".equals(method)){
			topublishRecord(request,response);
		}else if("edit".equals(method)){
			String httpMethod  = request.getMethod();
			if("POST".equals(httpMethod)){
				edit(request,response);
			}else if("GET".equals(httpMethod)){
				toedit(request,response);
			}	
		}else if("del".equals(method)){
			todel(request,response);
		}else if("add".equals(method)){
			String httpMethod  = request.getMethod();
			if("POST".equals(httpMethod)){
				add(request,response);
			}else if("GET".equals(httpMethod)){
				toadd(request,response);
			}
		}else if("singleContactXml".equals(method)){
			tosingleContactXml(request,response);
		}else {
			response.sendError(404);
			
			
		}
	}

	
	
	private void tosingleContactXml(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String p = request.getParameter("p");
		Page<Record> list = service.findContactAndRecord(id,p);
		Contact contact = service.findContact(id);
		
		if(contact == null){
			response.sendRedirect("contact.jspx?code=1004");
			return;
		}else{
			response.setContentType("text/xml;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			out.print("<contact >");
			out.print("<id>"+contact.getId()+"</id>");
			out.print("<name>"+contact.getName()+"</name>");
			out.print("<company>"+contact.getCompany()+"</company>");
			out.print("<cellphone>"+contact.getCellphone()+"</cellphone>");
			if(contact.getTelphone() != null){
				out.print("<tel>"+contact.getTelphone()+"</tel>");
			}else{
				out.print("<tel></tel>");
			}
			out.print("<address>"+contact.getAddress()+"</address>");
			if(contact.getEmail()!=null){
				out.print("<email>"+contact.getEmail()+"</email>");
				
			}else{
				out.print("<email></email>");
			}
			if(contact.getMainpage() != null){
				out.print("<mainpage>"+contact.getMainpage()+"</mainpage>");
			}else{
				out.print("<mainpage></mainpage>");
			}
			if(contact.getWeb()!=null){
				out.print("<web>"+contact.getWeb()+"</web>");
			}else{
				out.print("<web></web>");
			}
			
			if(contact.getWebUrl()!=null){
				out.print("<weburl>"+contact.getWebUrl()+"</weburl>");
				
			}else{
				out.print("<weburl></weburl>");
			}
			out.print("<recordlist>");
			for(Record record:list.getList()){
				
				out.print("<item>");
				out.print("<recordcontent>"+record.getContent()+"</recordcontent>");
				out.print("<recordname>"+record.getUser().getUsername()+"</recordname>");
				out.print("<createtime>"+record.getCreatetime()+"</createtime>");
				out.print("</item>");
				
				
			}
			out.print("</recordlist>");
			out.print("</contact>");
			out.flush();
			out.close();
			
			
		}
		
	}



	private void toadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/new_contact.jsp").forward(request, response);
		
		
	}



	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String address = request.getParameter("address");
		String cellphone = request.getParameter("cellphone");
		String telphone = request.getParameter("telphone");
		String email = request.getParameter("email");
		String introduce = request.getParameter("introduce");
		String mainpage = request.getParameter("mainpage");
		String web = request.getParameter("web");
		String url = request.getParameter("url");
		HttpSession session = request.getSession();
		String projectid = (String) session.getAttribute("projectid");
		service.saveContact(company,name,address,cellphone,telphone,email,introduce,mainpage,web,url,projectid);
		response.sendRedirect("contact.jspx?method=add&code=1006");
		
	}



	private void todel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		service.del(id);
		response.sendRedirect("contact.jspx");
		
	}



	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String contactid = request.getParameter("id");
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String address = request.getParameter("address");
		String cellphone = request.getParameter("cellphone");
		String telphone = request.getParameter("telphone");
		String email = request.getParameter("email");
		String introduce = request.getParameter("introduce");
		String mainpage = request.getParameter("mainpage");
		String web = request.getParameter("web");
		String url = request.getParameter("url");
		service.editContact(company,contactid,name,address,cellphone,telphone,email,introduce,mainpage,web,url);
		response.sendRedirect("contact.jspx?method=singleContact&id="+contactid);
	}
	private void toedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Contact contact = service.findContact(id);
		request.setAttribute("editcontact",contact);
		request.getRequestDispatcher("WEB-INF/views/edit_contact.jsp").forward(request, response);
		
	}
	private void topublishRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String content = request.getParameter("content");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		Contact contact = (Contact) session.getAttribute("singleContact");
		service.saveRecord(user,content,contact.getId());
		response.sendRedirect("contact.jspx?method=toAllRecord&id="+contact.getId());
		
	}
	private void toAllRecord(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String p = request.getParameter("p");
		String id = request.getParameter("id");
		Contact contact = service.findContact(id);
		if(contact != null){
			Page<Record> list = service.findContactAndRecord(id,p);
			contact.setList(list);
			HttpSession session = request.getSession();
			session.setAttribute("singleContact", contact);
			request.getRequestDispatcher("WEB-INF/views/contact_note.jsp").forward(request, response);
			return;
		}else{
			response.sendError(404,"不存在此联系人");
		}
		
		
		
		
		
	}
	private void toSingleContact(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String id = request.getParameter("id");
		String p = request.getParameter("p");
		Page<Record> list = service.findContactAndRecord(id,p);
		Contact contact = service.findContact(id);
		if(contact == null){
			response.sendRedirect("contact.jspx?code=1004");
			return;
		}else{
			contact.setList(list);
			HttpSession session = request.getSession();
			session.setAttribute("contactRecord", contact);
			response.sendRedirect("contact.jspx?id="+id);
			
		}
		
		
		
	}
	/**
	 * 进入到联系人页面
	 * */
	private void toContact(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String p = request.getParameter("p");
		HttpSession session = request.getSession();
		String projectid = (String) session.getAttribute("projectid");
		Page<Contact> contacList = service.findAllContactByProjectId(projectid,p);
		if(contacList.getList() .size() != 0){
			Contact contact = contacList.getList().get(0);
			Page<Record> list = service.findContactAndRecord(contact.getId(),p);
			contact.setList(list);
			request.setAttribute("contactMoren", contact);
			
		}
		
		request.setAttribute("contactList", contacList);
		request.getRequestDispatcher("WEB-INF/views/contact.jsp").forward(request,response);
		
	}
	
}
